/**
 * User.java
 * cn.feigme.identity.model
 * @author Feigme 
 * 2008-2-6
 */
package cn.feigme.identity.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

/**
 * @author Feigme 
 *
 */
@Entity
@Table(name="pf_user_tbl")
@Indexed(index="pf_user_tbl")
public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private String id;
	private String email;
	private String password;
	private String name;
	private String question;
	private String answer;
	private Boolean enabled;
	private String enableCode;
	
	private Set<LoginHistory> loginHistorys;
	
	public String getEmail() {return email;}
	public void setEmail(String email) {this.email = email;}

	@Id   
    @GeneratedValue(generator = "system-uuid")   
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @DocumentId
	public String getId() {return id;}
	public void setId(String id) {this.id = id;}
	
	public String getPassword() {return password;}
	public void setPassword(String password) {this.password = password;}

	@Field(index=Index.TOKENIZED,store=Store.NO)
	public String getName() {return name;}
	public void setName(String name) {this.name = name;}

	public String getQuestion() {return question;}
	public void setQuestion(String question) {this.question = question;}
	
	@Field(index=Index.TOKENIZED, store=Store.NO)
	public String getAnswer() {return answer;}
	public void setAnswer(String answer) {this.answer = answer;}

	@Column(columnDefinition="CHAR(1)")
	public Boolean getEnabled() {return enabled;}
	public void setEnabled(Boolean enabled) {this.enabled = enabled;}
	
	public String getEnableCode() {return enableCode;}
	public void setEnableCode(String enableCode) {this.enableCode = enableCode;}
	
	/**
	 * @return String
	 * @tags 如果name不存在就返回email，否则返回name
	 */
	@Transient
	public String getUserName(){
		if(this.getName()==null || "".equals(this.getName())){
			return this.getEmail();
		}else{
			return this.getName();
		}
	}

	@OneToMany(mappedBy="user",fetch=FetchType.LAZY)
	@Cascade(CascadeType.SAVE_UPDATE)
	public Set<LoginHistory> getLoginHistorys() {return loginHistorys;}
	public void setLoginHistorys(Set<LoginHistory> loginHistorys) {this.loginHistorys = loginHistorys;}
}
